package com.situ.mvc.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value="/fileUpload")
public class FileUploadController {

	//MultipartFile是SpringMVC帮我们封装的和文件上传相关的信息（文件名、路径、大小）
	@RequestMapping(value="/upload")
	public void upload(String name, MultipartFile multipartFile) {
		System.out.println(name);//张三
		System.out.println(multipartFile.getContentType());// image/png
		System.out.println(multipartFile.getName());//multipartFile
		System.out.println(multipartFile.getOriginalFilename());//无标题.png
		System.out.println(multipartFile.getSize());// 50760
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			inputStream = multipartFile.getInputStream();
			outputStream = new FileOutputStream(new File("D:/"+ multipartFile.getOriginalFilename()));
			IOUtils.copy(inputStream, outputStream);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(outputStream);
			IOUtils.closeQuietly(inputStream);
		}
	}
}
